﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using DBUtil;
using ZXCompany.Models.Entity;
namespace ZXCompany.Models.Dao
{
    public class ZX_DifferentDao
    {
        private Database db = new Database();
        /// <summary>
        /// 获取差别化信息
        /// </summary>
        /// <param name="sconn"></param>
        /// <returns></returns>
        public ZX_Different Select(SqlConnection sconn)
        {
            try
            {
                string sql = @"SELECT top 1 isnull([ID],0) as [ID]
                                      ,isnull([differentinfo],'') as [differentinfo]
                                      ,isnull([addUserID],0) as [addUserID]
                                      ,isnull([addTime],'') as [addTime]
                                      ,isnull([updateTime],'') as [updateTime]
                                  FROM [ZX_Different]";
                DataTable dt = db.Squery(sql,sconn);
                ZX_Different zd = new ZX_Different();
                if (dt!=null&&dt.Rows.Count > 0)
                {
                    zd.addTime = dt.Rows[0]["addTime"].ToString();
                    zd.addUserID = Convert.ToInt32(dt.Rows[0]["addUserID"]);
                    zd.differentinfo = dt.Rows[0]["differentinfo"].ToString();
                    zd.ID = Convert.ToInt32(dt.Rows[0]["ID"]);
                    zd.updateTime = dt.Rows[0]["updateTime"].ToString();
                }
                return zd;
            }
            catch (Exception ex)
            {
                throw ex;
            }  
        }

        /// <summary>
        /// 数据插入
        /// </summary>
        /// <param name="cinfo"></param>
        /// <param name="sconn"></param>
        public void Insert(ZX_Different cinfo, SqlConnection sconn)
        {
            string sqlstr = @"INSERT INTO [ZX_Different]
                           ([differentinfo]
                           ,[addUserID]
                           ,[addTime])
                     VALUES
                           (@differentinfo
                           ,@addUserID
                           ,@addTime)";
            SqlParameter[] para = new SqlParameter[]
            {
                new SqlParameter("@differentinfo",cinfo.differentinfo),
                new SqlParameter("@addUserID",cinfo.addUserID),
                new SqlParameter("@addTime",cinfo.addTime)
            };
            try
            {
                db.Udaquery(sqlstr, sconn, para);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        /// <summary>
        /// 数据更新
        /// </summary>
        /// <param name="cinfo"></param>
        /// <param name="sconn"></param>
        public void Update(ZX_Different cinfo, SqlConnection sconn)
        {
            string sqlstr = @"update [ZX_Different]
                           set [differentinfo] =@differentinfo,
                               updateTime =@updateTime
                           where id=@id";
            SqlParameter[] para = new SqlParameter[]
            {
                new SqlParameter("@differentinfo", cinfo.differentinfo ),
                new SqlParameter("@updateTime",cinfo.updateTime),
                new SqlParameter("@id",cinfo.ID)
            };
            try
            {
                db.Udaquery(sqlstr, sconn, para);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
    }
}